﻿using AutoMapper;

using EmployeeCard.Common.Attributes;
using EmployeeCard.IRepository;
using EmployeeCard.IServices;
using EmployeeCard.Model.Models;
using EmployeeCard.Model.ViewModels;
using EmployeeCard.Services.Base;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EmployeeCard.Services
{
    [CachingInterceptor]
    public class BlogArticleServices : BaseService<BlogArticle>, IBlogArticleServices
    {
        private IBlogArticleRepository _dal;
        private IMapper _mapper;
        public BlogArticleServices(IBlogArticleRepository dal, IMapper mapper)
        {
            this._dal = dal;
            base._baseDal = dal;
            this._mapper = mapper;
        }

        /// <summary>
        /// 获取视图博客详情信息(一般版本)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<BlogViewModels> GetBlogDetails(int id)
        {
            var bloglist = await _dal.Query(a => a.bID > 0, a => a.bID);
            var blogArticle = (await _dal.Query(a => a.bID == id)).FirstOrDefault();
            BlogViewModels models = null;

            if (blogArticle != null)
            {
                BlogArticle prevblog;
                BlogArticle nextblog;
                int blogIndex = bloglist.FindIndex(item => item.bID == id);
                if (blogIndex >= 0)
                {
                    try
                    {
                        // 上一篇
                        prevblog = blogIndex > 0 ? (((BlogArticle)(bloglist[blogIndex - 1]))) : null;
                        // 下一篇
                        nextblog = blogIndex + 1 < bloglist.Count() ? (BlogArticle)(bloglist[blogIndex + 1]) : null;

                        models = _mapper.Map<BlogViewModels>(blogArticle);
                        //models = new BlogViewModels()
                        //{
                        //    bsubmitter = blogArticle.bsubmitter,
                        //    btitle = blogArticle.btitle,
                        //    bcategory = blogArticle.bcategory,
                        //    bcontent = blogArticle.bcontent,
                        //    btraffic = blogArticle.btraffic,
                        //    bcommentNum = blogArticle.bcommentNum,
                        //    bUpdateTime = blogArticle.bUpdateTime,
                        //    bCreateTime = blogArticle.bCreateTime,
                        //    bRemark = blogArticle.bRemark,
                        //};

                        if (nextblog != null)
                        {
                            models.next = nextblog.btitle;
                            models.nextID = nextblog.bID;
                        }
                        if (prevblog != null)
                        {
                            models.previous = prevblog.btitle;
                            models.previousID = prevblog.bID;
                        }
                    }
                    catch (Exception) { }
                }
                blogArticle.btraffic += 1;
                await _dal.Update(blogArticle, new List<string> { "btraffic" });
            }

            return models;

        }

        /// <summary>
        /// 获取博客列表
        /// </summary>
        /// <returns></returns>
        [Caching(AbsoluteExpiration =10)]
        public async Task<List<BlogArticle>> GetBlogs()
        {
            var blogList = await _dal.Query(x => x.bID > 0, a => a.bID);
            return blogList;
        }
    }
}
